package api

import (
	"net/http"
	"gitee.com/DengAnbang/streamWeb/api/defs"
	"gitee.com/DengAnbang/streamWeb/api/dbops"
	"gitee.com/DengAnbang/streamWeb/public/session"
	"os"
	"time"
	"gitee.com/DengAnbang/goutils/fileUtil"
	"io/ioutil"
	"html/template"
	"gitee.com/DengAnbang/streamWeb/api/utils"
)

func CreateUser(w http.ResponseWriter, r *http.Request) error {
	loginName := r.Form.Get("login_name")
	pwd := r.Form.Get("pwd")

	if len(loginName) == 0 {
		return defs.NewResultError(defs.ParameterErr, "登陆名字不能为空")
	}
	if len(pwd) == 0 {
		return defs.NewResultError(defs.ParameterErr, "密码不能为空")
	}
	bean, err := dbops.GetUser(loginName)
	if err != nil {
		return err
	}
	if !bean.IsEmpty() {
		return defs.NewResultError(defs.ExistErr, "已经存在了")
	}
	user, err := dbops.AddUser(loginName, pwd)
	if err != nil {
		return err
	}
	newSession, err := session.CreateNewSession(user.UserId)
	if err != nil {
		return err
	}
	user.SessionId = newSession.SessionID
	return defs.NewResultData(user)
}

func LoginUser(w http.ResponseWriter, r *http.Request) error {
	loginName := r.Form.Get("login_name")
	pwd := r.Form.Get("pwd")
	if len(loginName) == 0 {
		return defs.NewResultError(defs.ParameterErr, "登陆名字不能为空")
	}
	if len(pwd) == 0 {
		return defs.NewResultError(defs.ParameterErr, "密码不能为空")
	}
	user, err := dbops.GetUser(loginName)
	if err != nil {
		return err
	}
	if user.Pwd != pwd {
		return defs.NewResultError(defs.VerifyErr, "账号不存在或密码错误")
	}
	s, err := dbops.GetSessionIdByUid(user.UserId)
	err = session.DeleteSession(s)
	if err != nil {
		return err
	}
	newSession, err := session.CreateNewSession(user.UserId)
	if err != nil {
		return err
	}
	user.SessionId = newSession.SessionID
	return defs.NewResultData(user)
}
func videoPlay(w http.ResponseWriter, r *http.Request) error {
	r.ParseForm()
	videoId := r.Form.Get("video_id")
	if len(videoId) == 0 {
		return defs.NewResultError(defs.ParameterErr, "video_id 为空")
	}
	videoBean, err := dbops.GetVideo(videoId)
	if err != nil {
		return err
	}
	vl := defs.VIDEO_DIR + videoId + "/" + videoBean.Name
	if !fileUtil.PathExists(vl) {
		return defs.NewResultError(defs.InternalErr, "文件不存在!")
	}
	file, err := os.Open(vl)
	if err != nil {
		return err
	}
	defer file.Close()
	w.Header().Set("Content-Type", "video/mp4")
	http.ServeContent(w, r, videoBean.Name, time.Now(), file)
	return nil
}
func videoUpload(w http.ResponseWriter, r *http.Request) error {
	//userId := r.Form.Get("user_id")
	//if len(userId) == 0 {
	//	return defs.NewResultError(defs.ParameterErr, "错误的请求").SetDebugMeg("请传入userID")
	//}
	r.Body = http.MaxBytesReader(w, r.Body, defs.VIDEO_MAX_BYTE)
	err := r.ParseMultipartForm(defs.VIDEO_MAX_BYTE)
	if err != nil {
		return defs.NewResultError(defs.NormalErr, "解析错误或者文件太大!").SetDebugMeg(err.Error())
	}
	file, h, err := r.FormFile("file")
	if err != nil {
		return err
	}
	bytes, err := ioutil.ReadAll(file)
	if err != nil {
		return err
	}
	uuid, _ := utils.NewUUID()
	os.MkdirAll(defs.VIDEO_DIR+uuid+"/", 0x666)
	err = ioutil.WriteFile(defs.VIDEO_DIR+uuid+"/"+h.Filename, bytes, 0x666)
	if err != nil {
		return err
	}
	videoBean, err := dbops.AddVideo(uuid, "10014", h.Filename)
	if err != nil {
		return err
	}
	return defs.NewResultData(videoBean)
}
func testUpload(w http.ResponseWriter, r *http.Request) error {
	t, err := template.ParseFiles(defs.VIEW_DIR + "upload.html")
	if err != nil {
		return err
	}
	t.Execute(w, nil)
	return nil
}
